<template>
  <div>
    <page-list :x-uri="uri" :x-tabs="tabs" :doactions="doactions" :tree="true" :lazy="true" :lazy-fun="loadChild" :custom-components="custom">
      <!-- 页面操作按钮 -->
      <template v-slot:page-button>
        <el-button :disabled="!isFinished" size="mini" type="primary" @click="create">新建课时</el-button>
        <el-button v-if="isFinished" size="mini" type="primary" @click="finish">完结课时</el-button>
      </template>
    </page-list>
    <sort-dialog :value.sync="sortValue" :visible.sync="visible" :disable-submit="disableSubmit" @submit="sortSubmit" />
  </div>
</template>

<script>
import PageList from '@public/template/page_list.vue';
import Price from './components/price.vue';
import Title from './components/title.vue';
import SortDialog from '@/components/update-sort-dialog';
import { mapGetters } from 'vuex';

export default {
  components: {
    PageList,
    SortDialog
  },
  inject: ['reload'],
  data() {
    return {
      uri: 'course/admin/video/' + this.$route.params.id + '/section',
      custom: {
        price: Price,
        title: Title
      },
      tabs: [
        {
          title: '点播列表',
          uri: '/course/video/list',
          meta: {
            permission: 'course.video.default'
          }
        },
        {
          title: '课时列表',
          uri: '/course/video/' + this.$route.params.id + '/section',
          meta: {
            permission: 'course.video.default.section'
          }
        }
      ],
      visible: false,
      itemId: 0,
      disableSubmit: false,
      sortValue: 1, // 排序值
      doactions: [
        {
          title: '排序',
          meta: { permission: 'site.pc.personal.menu.sort' },
          callback: this.sortPopover,
          display({ row }) {
            return row.is_last;
          }
        },
        {
          title: '添加课时',
          // meta: { permission: 'course.video.section' },
          callback: this.create,
          display: ({ row }) => {
            return this.isFinished && row.video_sec_level > row.level;
          }
        },
        { title: '编辑', callback: this.edit },
        {
          title: '删除',
          callback: this.deleteSection,
          display: ({ row }) => {
            return this.isFinished;
          }
        }
      ]
    };
  },
  computed: {
    ...mapGetters('pageList', ['response']),
    isFinished() {
      return !this.response.is_finished;
    }
  },
  methods: {
    // 排序弹出层
    sortPopover({ row }) {
      this.visible = true;
      this.itemId = row.id;
      this.sortValue = row.best_sort;
    },
    // 保存排序
    sortSubmit() {
      this.disableSubmit = true;
      this.$http.put(
        '/course/admin/video/' + this.$route.params.id + '/section/' + this.itemId + '/sort',
        {
          best_sort: this.sortValue
        },
        ({ msg }) => {
          this.$message.success(msg);
          this.disableSubmit = false;
          this.visible = false;
          this.reload();
        },
        ({ msg }) => {
          this.$message.error(msg);
          this.disableSubmit = false;
        },
        false
      );
    },
    finish: function() {
      this.$box
        .confirm('完结课时后将不能添加或删除课时，确认完结吗')
        .then(() => {
          this.$http.post(
            '/course/admin/video/' + this.$route.params.id + '/finished',
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    // 新建课时
    create: function({ row }) {
      const pid = row ? row.id : 0;
      this.$router.push({ path: '/course/video/' + this.$route.params.id + '/section/' + pid + '/create' });
    },
    // 编辑课时
    edit: function({ row }) {
      const sid = row ? row.id : 0;
      this.$router.push({ path: '/course/video/' + this.$route.params.id + '/section/' + sid + '/edit' });
    },
    deleteSection: function({ row }) {
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/course/admin/video/' + this.$route.params.id + '/section/' + row.id,
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    loadChild: function(row, treeNode, resolve) {
      this.$http.get(
        'course/admin/video/' + this.$route.params.id + '/section/' + row.id,
        {
          pid: row.id
        },
        ({ data }) => {
          resolve(data);
        },
        response => {
          resolve([]);
          this.$message.error(response.msg);
        },
        false
      );
    }
  }
};
</script>
